package com.hoshiibuy.sys.permission.controller;

import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.sys.permission.entity.SysConfig;
import com.hoshiibuy.sys.permission.entity.param.SysConfigInsertParam;
import com.hoshiibuy.sys.permission.service.impl.SysConfigServiceImpl;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 全站配置信息表 前端控制器
 * </p>
 *
 * @author Mr.Wu
 * @since 2019-04-19
 */
@Api(tags = "全站配置接口")
@RestController
@RequestMapping("/config")
public class SysConfigController {

	private SysConfigServiceImpl sysConfigService;

	@ApiOperation("新增配置")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Response addSysConfig(SysConfigInsertParam param, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		SysConfig config = new SysConfig();
		// 调用内置工具产生ID
		config.setId(Integer.valueOf(IdWorker.getIdStr().subSequence(0, 8).toString()));
		// TODO 只接受指定参数
		boolean success = sysConfigService.save(config);
		if (success) {
			return Response.success("新增成功");
		} else {
			return Response.failed("新增失败");
		}
	}

	@ApiOperation("修改配置")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public Response updateSysConfig(SysConfig param) {
		boolean success = sysConfigService.updateById(param);
		if (success) {
			return Response.success("修改成功");
		} else {
			return Response.failed("修改失败");
		}
	}

	@ApiOperation("删除配置")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Response deleteSysConfig(@RequestParam String id) {
		if (StringUtils.isBlank(id)) {
			return Response.failed("id不能为空");
		}
		boolean success = sysConfigService.removeById(id);
		if (success) {
			return Response.success("删除成功");
		} else {
			return Response.failed("删除失败");
		}
	}

	@ApiOperation("获取配置详情")
	@RequestMapping(value = "/getDetail", method = RequestMethod.GET)
	public Response<SysConfig> getSysConfigDetail(@RequestParam String id) {
		SysConfig dept = sysConfigService.getById(id);
		if (dept != null) {
			return Response.data(dept);
		} else {
			return Response.failed("数据不存在");
		}
	}

	@ApiOperation("获取配置列表")
	@RequestMapping(value = "getList", method = RequestMethod.GET)
	public Response<List<SysConfig>> getSysConfigList(@Valid SysConfig param, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		QueryWrapper<SysConfig> queryWrapper = new QueryWrapper<SysConfig>(param);
		List<SysConfig> list = sysConfigService.list(queryWrapper);
		return Response.data(list);
	}
}
